package com.hygj.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hygj.pojo.Print;
import com.hygj.service.PrintService;
import com.hygj.util.PageUtil;

@Controller
@RequestMapping
public class PrintController {

	@Autowired
	private PrintService printService;
	
	/**
	 * 保存
	 * 
	 * @param print
	 * @return
	 */

	@RequestMapping(value = "insertPrint", method = RequestMethod.POST)
	public String insertPost(Map<String, Object> map, Print print) {
		printService.insert("insert", print);
		map.put("print", print);
		return "prints/printOne";
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("deletePrint")
	public String deletePrint(@RequestParam("id") Integer id) {
		printService.delete("delete", id);
		return "redirect:getAllPrint";
	}
    
	/**
	 * 查询全部
	 * 
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("getAllPrint")
	public String getAllPrint(@ModelAttribute("search") Print print, Map<String, Object> map,
			HttpServletRequest request) {
		Map<String, Object> params = new HashMap<String, Object>();
		// 判断长度，是否为空
		if (StringUtils.isNotBlank(print.getPrint_name())) {
			params.put("print_name", "%" + print.getPrint_name() + "%");

		}
		PageUtil<Print> page = printService.page(request, 10, params);
		map.put("page", page);
		return "prints/printShow";
	}

	/**
	 * 详情页面
	 * 
	 * @param id
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("getPrintById")
	public String getPrintById(@RequestParam("id") Integer id, ModelMap modelMap) {
		Print prints = printService.getById("getById", id);
		modelMap.addAttribute("prints", prints);
		return "prints/printDetails";
	}

	/**
	 * 打印页面
	 * 
	 * @param id
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("getPrintByIds")
	public String getPrintByIds(@RequestParam("id") Integer id, ModelMap modelMap) {
		Print prints = printService.getById("getById", id);
		modelMap.addAttribute("prints", prints);
		return "prints/printTwo";
	}
	
	
}
